
import LanguageMng from "./LanguageMng";
cc.Class({
    extends: cc.Component,

    properties: {
        dealInfo:{
            default:null,
            type:cc.Node
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        //
        // cc.loader.loadRes("windows/DealInfo",cc.Prefab,function (err, prefab) {
        //     if(err){
        //         cc.error(err)
        //         return
        //     }
        //
        //     let dealInfo = cc.instantiate(prefab)
        //     cc.find("Canvas").addChild(dealInfo)
        //     this.dealInfo = dealInfo
        // }.bind(this))
    },

    start () {
        // cc.gg = {}
        // let debug_log_flow_show = true
        // let debug_log_drag_poker = false
        // let debug_log_message = true
        // let debug_log_calculate = false
        // let debug_log_bottoms = false
        // let debug_log_grab = true
        // let debug_log_ws = true
        //
        // cc.logcal = function (...vars) {
        //     if(debug_log_calculate)
        //         cc.log(...vars)
        // }
        //
        // cc.logfl = function (...vars) {
        //     if(!debug_log_flow_show)
        //         return
        //     cc.log("========flow:",...vars)
        // }
        //
        // cc.logdrag = function (...vars) {
        //     if(debug_log_drag_poker)
        //         cc.log("Log Drag:",...vars)
        // }
        //
        // cc.logmsg = function (...vars) {
        //     if(debug_log_message)
        //         cc.log(...vars)
        // }
        //
        // cc.logbot = function (...vars) {
        //     if(debug_log_bottoms)
        //         cc.log(...vars)
        // }
        //
        // cc.loggrab = function (...vars) {
        //     if(debug_log_grab)
        //         cc.log("grab:",...vars)
        // }
        //
        // cc.logws = function (...vars) {
        //     if(debug_log_ws)
        //         cc.log("ws:",...vars)
        // }
        //
        // let ResourceUtil = require("ResourceUtil")
        // cc.gg.resUtil = new ResourceUtil()
        //
        // let HttpUtil = require("HttpUtil")
        // cc.gg.httpUtil = new HttpUtil()
        //
        // let DataMgr = require("DataMgr")
        // cc.gg.dataMgr = new DataMgr()
        //
        // let NetMgr = require("NetMgr")
        // cc.gg.netMgr = new NetMgr()
        //
        // let EnumConfig = require("EnumConfig")
        // cc.gg.enumConfig = new EnumConfig()
        //
        // let BaseUtil = require("BaseUtil")
        // cc.gg.baseUtil = new BaseUtil()
        //
        // let SortUtil = require("SortUtil")
        // cc.gg.sortUtil = new SortUtil()
        //
        // let i18n = require("i18n")
        // cc.gg.i18n = new i18n()
        //
        // let ToastMgr = require("ToastMgr")
        // cc.gg.toastMgr = new ToastMgr()
        //
        // let GameDataUtil = require("GameDataUtil")
        // cc.gg.gameDataUtil = new GameDataUtil
        //
        // let GameLogicUtil = require("GameLogicUtil")
        // cc.gg.logicUtil = new GameLogicUtil()
    },

    // update (dt) {},

    onTestButtonClicked:function (event) {
        let btnName = event.target.name
        if(btnName === "b1"){
            cc.gg.dataMgr.users = []
            cc.gg.dataMgr.users[1] = {uid:1,name:"Squall"}
            cc.gg.dataMgr.users[2] = {uid:2,name:"Rinoa"}

            this.dealInfo.active = true
            this.dealInfo.getComponent("DealInfo").initDealInfo(cc.gg.dataMgr.users,1)
        }else if(btnName === "b2"){
            let users = {}
            users[1] = {
                uid:1,
                score:11,
                cards:[
                    [0x06,0x07,0x08,0x09],
                    [0x0c,0x18,0x4e],
                    [0x1b,0x1c,0x1d],
                    [0x27,0x26,0x4f]
                ]}

            this.dealInfo.getComponent("DealInfo")._onUpdateDealInfo(users[1],1,2,users)
        }else if(btnName === "b3"){
            let users = {}
            users[2] = {
                uid:2,
                name:"Rinoa",
                cards:[
                    [0x06,0x07,0x08,0x09],
                    [0x0c,0x1c,0x2c],
                    [0x1b,0x1c,0x1d],
                    [0x27,0x26,0x4f],
                    [0x01]
                ],
                score:-11,
            }
            this.dealInfo.getComponent("DealInfo")._onUpdateDealInfo(users[2],1)
        }else if(btnName === "b4"){
            let title = "测试"
            let positiveStr = "Lobby"
            let passiveStr = "Cancel"
            cc.gg.toastMgr.showDialog(title,positiveStr,passiveStr,"这个是测试用的，看看能不能关掉",
                function () {
                    cc.gg.gameDataUtil.goToLobby(true)
                }.bind(this),
                function () {
                    cc.gg.toastMgr.removeDialog()
                }
            )
        }else if(btnName === "b5"){
            this._initTestSeat()
        }else if(btnName === "b6"){
            let img = "face_1.png"
            this._initTestSeat({name:"李云龙",score:30,uid:1,img,face_bg:"020230"})
        }else if(btnName === "b7"){
            let img = "face_2.png"
            this._userEnter({name:"李云飞",score:30,uid:2,img,face_bg:"820230"})
        }else if(btnName === "b8"){
            cc.find("Canvas/seat_other").getComponent("SeatOther").startCountting(60)
        }else if(btnName === "b9"){
            cc.gg.baseUtil.setLandscape(2)
            cc.director.loadScene("WebScene")
        }else if(btnName === "b10"){
            this._userOut()
        }else if(btnName === "b11"){
            cc.gg.baseUtil.setLandscape(2)
            cc.director.loadScene("WebScene",function () {
                cc.find("Canvas").getComponent("WebScene").webNode.getComponent(cc.WebView).url = "http://static.khelochalo.com/payresult.html"
            })
        }else if(btnName === "b12"){
            this.OnJSCallback()
        }else if(btnName === "b13"){
            cc.gg.httpUtil.httpGet("/order/payment/web/test/link/get",{packId:1},function (result) {
                if(!result.succ){
                    cc.logfl("在房间内重连失败，回到大厅")
                    // cc.gg.toastMgr.showToast("Reconnect Failed.")
                    cc.gg.gameDataUtil.goToLobby(true)
                    return
                }

                let link = result.data.link
                cc.gg.baseUtil.setLandscape(2)
                cc.director.loadScene("WebScene",function () {
                    cc.find("Canvas").getComponent("WebScene").webNode.getComponent(cc.WebView).url = link
                })
            }.bind(this))
        }else if(btnName === "b14"){
            let cards = [
                    [0x06,0x07,0x08,0x09],
                    [0x0c,0x1c,0x2c],
                    [0x1b,0x1c,0x1d],
                    [0x27,0x26,0x4f],
                    [0x01]
                ]
            this.dealInfo.getComponent("DealInfo").updateUserCards(2,cards,1)
        }else if(btnName === "b15"){
            this.dealInfo.getComponent("DealInfo").updateUserScores(2,5,"")
        }else if(btnName === "b16"){
            let cards = [
                [0x01,0x02,0x03,0x04],
                [0x0c,0x1c,0x2c],
                [0x1b,0x1c,0x1d],
                [0x27,0x26,0x4f],
                [0x01]
            ]
            this.dealInfo.getComponent("DealInfo").updateUserCards(1,cards,1)
        }else if(btnName === "b17"){
            this.dealInfo.getComponent("DealInfo").updateUserScores(1,-5,100)
        }else if(btnName === "b18"){
            this.dealInfo.getComponent("DealInfo").setWinUser(2)
        }else if(btnName === "b19"){
            cc.find("Canvas/seat_other").getComponent("SeatOther").changeToEliminated()
        }else if(btnName === "b20"){
            cc.find("Canvas/seat_other").getComponent("SeatOther").changeToDrop()
        }
    },


    OnJSCallback: function(sender, url) {
        cc.log("JS回调 = " + url);
        url = "cocosjs://key=payment_result&status=1&errmsg=%E6%94%AF%E4%BB%98%E5%B7%B2%E5%AE%8C%E6%88%90"
        let prefix = "cocosjs://"
        let content = url.substr(prefix.length)
        let arr = content.split("&")
        let array = {}
        for(let i=0;i<arr.length;i++){
            let tmpArr = arr[i].split("=")
            array[tmpArr[0]] = tmpArr[1]
        }
        cc.log("解析完后：",JSON.stringify(array))
        if(array["key"] === "payment_result"){
            let status = array["status"]
            let errmsg = array["errmsg"]
            if(status == 1){
                let title = LanguageMng.getSentence(3)
                let btnStr = LanguageMng.getSentence(57)
                let contentStr = LanguageMng.getSentence(168)
                let cb = function () {
                    cc.gg.baseUtil.setLandscape(1)
                    cc.director.loadScene("LobbyBlue")
                }.bind(this)
                cc.gg.toastMgr.showDialogOneButton(title,btnStr,contentStr,cb)
            }else {
                let title = LanguageMng.getSentence(3)
                let btnStr = LanguageMng.getSentence(57)
                let contentStr = LanguageMng.getSentence(169)
                let cb = function () {
                    // cc.gg.baseUtil.setLandscape(1)
                    // cc.director.loadScene("LobbyBlue")
                    cc.gg.toastMgr.removeDialog()
                }.bind(this)
                cc.gg.toastMgr.showDialogOneButton(title,btnStr,contentStr,cb)
            }
        }else {
            cc.error("Unknown js callback:",array["key"])
        }
    },

    _initTestSeat:function (userObj) {
        cc.find("Canvas/seat_other").getComponent("SeatOther").initSeat(userObj)
    },

    _userEnter:function (userObj) {
        cc.find("Canvas/seat_other").getComponent("SeatOther").enterUser(userObj)
    },

    _userOut:function () {
        cc.find("Canvas/seat_other").getComponent("SeatOther").userOut()
    }
});
